/**
 *  dblclick  click mouseleave mouseenter dragstart drag dragend
 */
class Event {
	constructor(viewer) {
		this._viewer = viewer;
		this._camera = viewer.camera;
		this.LEFT_CLICK_MAP = new Map();
		this.LEFT_DOUBLE_CLICK_MAP = new Map();
		this.RIGHT_CLICK_MAP = new Map();
		this.MOUSE_MOVE_MAP = new Map();
		this.MOUSE_LEAVE_MAP = new Map();
		this.MOUSE_ENTER_MAP = new Map();
		this.MOVE_START_MAP = new Map();
		this.MOVE_MAP = new Map();
		this.MOVE_END_MAP = new Map();
		this.init();
	}
	init() {
		// 鼠标左键点击事件
		this._handler = new Cesium.ScreenSpaceEventHandler(this._viewer.canvas);
		let _this = this;
		this._handler.setInputAction(function(event) {
			if (Cesium.defined(event.position)) {
				if (_this.LEFT_CLICK_MAP.size > 0) {
					let thing = _this._viewer.scene.pick(event.position);
					_this.LEFT_CLICK_MAP.forEach(function(value, key) {
						_this._back(value,event,thing);
					});
				}
			}
		}, Cesium.ScreenSpaceEventType.LEFT_CLICK);
		
		this._handler.setInputAction(function(event) {
			if (Cesium.defined(event.position)) {
				if (_this.LEFT_DOUBLE_CLICK_MAP.size > 0) {
					let thing = _this._viewer.scene.pick(event.position);
					_this.LEFT_DOUBLE_CLICK_MAP.forEach(function(value, key) {
						_this._back(value,event,thing);
					});
				}
			}
		}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
		
		this._handler.setInputAction(function(event) {
			if (Cesium.defined(event.position)) {
				if (_this.MOVE_START_MAP.size > 0) {
					let thing = _this._viewer.scene.pick(event.position);
					_this.MOVE_START_MAP.forEach(function(value, key) {
						_this._back(value,event,thing);
					});
				}
			}
		}, Cesium.ScreenSpaceEventType.LEFT_DOWN);
		
		this._handler.setInputAction(function(event) {
			if (Cesium.defined(event.position)) {
				if (_this.MOVE_END_MAP.size > 0) {
					let thing = _this._viewer.scene.pick(event.position);
					_this.MOVE_END_MAP.forEach(function(value, key) {
						_this._back(value,event,thing);
					});
				}
			}
		}, Cesium.ScreenSpaceEventType.LEFT_UP);
		
		let hoverThing;
		// 鼠标移动事件
		this._handler.setInputAction(function(event) {
			if (_this.MOUSE_ENTER_MAP.size > 0 || _this.MOUSE_LEAVE_MAP.size > 0){
				let hoverThing = _this._viewer.scene.pick(event.endPosition);
				if(Cesium.defined(event.position)){
					_this.MOUSE_ENTER_MAP.forEach(function(value, key) {
						_this._back(value,event,thing);
					});
				}else{
					_this.MOUSE_LEAVE_MAP.forEach(function(value, key) {
						_this._back(value,event,thing);
					});
				}
			}
			_this.MOVE_MAP.forEach(function(value, key) {
				_this._back(value,event,null);
			});
		}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);

		// 鼠标右键点击事件
		this._handler.setInputAction(function(event) {
			if (Cesium.defined(event.position)) {
				if (_this.RIGHT_CLICK_MAP.size > 0) {
					let thing = _this._viewer.scene.pick(event.position);
					_this.RIGHT_CLICK_MAP.forEach(function(value, key) {
						_this._back(value,event,thing);
					});
				}
			}
		}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
	}
	
	/**
	 * @description 返回
	 * @param {Object} value
	 * @param {Object} event
	 * @param {Object} thing
	 */
	_back(value,event,thing){
		if (0 == value.backType) {
			value.callback({
				event: event,
				thing: thing
			});
		}
		else if (1 == value.backType) {
			value.callback({
				event: event,
			});
		}
		else if (2 == value.backType) {
			value.callback({
				thing: thing
			});
		}
		else{
			value.callback();
		}
	}
	
	/**
	 * @description 全局绑定事件
	 * @param {Object} type
	 * @param {Object} condition
	 * @param {Object} userData
	 * @param {Object} callback
	 * @param {Object} tag
	 * @param {Object} priority
	 */
	on(options, callback) {
		if (!Cesium.defined(options) || !Cesium.defined(options.type)) {
			console.error('参数有误！');
			return;
		}
		if ('LEFT_CLICK' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.LEFT_CLICK_MAP.set(options.condition, {
					callback: callback,
					backType: Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.LEFT_CLICK_MAP.set(options.type, {
				callback: callback,
				backType: Cesium.defaultValue( options.backType,null)
			});
			return;
		}
		
		if ('LEFT_DOUBLE_CLICK' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.LEFT_DOUBLE_CLICK_MAP.set(options.condition, {
					callback: callback,
					backType: Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.LEFT_DOUBLE_CLICK_MAP.set(options.type, {
				callback: callback,
				backType: Cesium.defaultValue( options.backType,null)
			});
			return;
		}
		
		if ('RIGHT_CLICK' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.RIGHT_CLICK_MAP.set(options.condition, {
					callback: callback,
					backType: Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.RIGHT_CLICK_MAP.set(options.type, {
				callback: callback,
				backType: Cesium.defaultValue( options.backType,null)
			});
			return;
		}
		
		if ('MOUSE_LEAVE' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.MOUSE_LEAVE_MAP.set(options.condition, {
					callback: callback,
					backType: Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.MOUSE_LEAVE_MAP.set(options.type, {
				callback: callback,
				backType: Cesium.defaultValue( options.backType,null)
			});
			return;
		}
		
		if ('MOUSE_ENTER' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.MOUSE_ENTER_MAP.set(options.condition, {
					callback: callback,
					backType: Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.MOUSE_ENTER_MAP.set(options.type, {
				callback: callback,
				backType: Cesium.defaultValue( options.backType,null)
			});
			return;
		}
		
		if ('MOVE_START' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.MOVE_START_MAP.set(options.condition, {
					callback: callback,
					backType: Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.MOVE_START_MAP.set(options.type, {
				callback: callback,
				backType: Cesium.defaultValue( options.backType,null)
			});
			return;
		}  
		
		if ('MOVE' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.MOVE_MAP.set(options.condition, {
					callback: callback,
					backType: Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.MOVE_MAP.set(options.type, {
				callback: callback,
				backType: Cesium.defaultValue( options.backType,null)
			});
			return;
		} 
		
		if ('MOVE_END' == options.type) {
			if (Cesium.defined(options.condition)) {
				this.MOVE_END_MAP.set(options.condition, {
					callback: callback,
					backType:Cesium.defaultValue( options.backType,null)
				});
				return;
			}
			this.MOVE_END_MAP.set(options.type, {
				callback: callback,
				backType:Cesium.defaultValue( options.backType,null)
			});
			return;
		}
		
	}

	/**
	 * @description 移除事件绑定
	 * @param {Object} type 事件类型名称
	 * @param {Object} condition 物体类型选择条件
	 * @param {Object} callback 事件触发的回调函数 或 事件标签（tag）
	 */
	off(options, callback) {
		if (!Cesium.defined(options) || !Cesium.defined(options.eventType)) {
			console.error('参数有误！');
			return;
		}
		if ('LEFT_CLICK' == options.type) {
		} else if ('LEFT_DOUBLE_CLICK' == options.type) {
		} else if ('RIGHT_CLICK' == options.type) {
		}
		if (callback) {
			callback();
		}
	}

	/**
	 * @description 暂停事件响应
	 * @param {Object} type 事件名称
	 * @param {Object} condition 物体类型选择条件
	 * @param {Object} tag 事件标签
	 */
	pause(eventType, condition, tag) {

	}

	/**
	 * @description 恢复事件响应
	 * @param {Object} eventType 事件名称
	 * @param {Object} condition 物体类型选择条件
	 * @param {Object} tag 事件标签
	 */
	resume(eventType, condition, tag) {

	}
	
	/**
	 * @description 暂停全部
	 */
	pauseAll(){
		if (this._handler) {
			this._handler.destroy();
			this._handler=null;
		}
	}
	
	/**
	 * @description 回复全部
	 */
	resumeAll(){
		if (this._handler==null){
			this.init();
		}
	}

	/**
	 * @description 销毁
	 */
	destroy() {
		if (this.LEFT_CLICK_MAP) {
			this.LEFT_CLICK_MAP.clear();
		}
		if (this.LEFT_DOUBLE_CLICK_MAP) {
			this.LEFT_DOUBLE_CLICK_MAP.clear();
		}
		if (this.RIGHT_CLICK_MAP) {
			this.RIGHT_CLICK_MAP.clear();
		}
		if (this.MOUSE_LEAVE_MAP) {
			this.MOUSE_LEAVE_MAP.clear();
		}
		if (this.MOUSE_ENTER_MAP) {
			this.MOUSE_ENTER_MAP.clear();
		}
		if (this.MOVE_START_MAP) {
			this.MOVE_START_MAP.clear();
		}
		if (this.MOVE_MAP) {
			this.MOVE_MAP.clear();
		}
		if (this.MOVE_END_MAP) {
			this.MOVE_END_MAP.clear();
		}
		if (this._handler) {
			this._handler.destroy();
		}
		
		delete this.LEFT_CLICK_MAP
		delete this.RIGHT_CLICK_MAP
		delete this.MOUSE_LEAVE_MAP
		delete this.MOUSE_ENTER_MAP
		delete this.MOVE_START_MAP
		delete this.MOVE_MAP
		delete this.MOVE_END_MAP
		delete this._handler
		delete this._viewer
		return Cesium.destroyObject(this);
	}

}

export {
	Event
}
